<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Time extends Common
{
    public $think;

    public function __construct()
    {
        parent::__construct();
        $this->think = $_SESSION['think'];
    }
    public function index()
    {
        return view();
    }
    /**
     * 发送指令到计时屏
     * @return string
     */
    public function sendTime()
    {
        $act = input('post.act');
        $host = input('post.host');//IP
        $port = input('post.port');//端口
        $length = strlen($act);
        if(!extension_loaded('sockets')){
            $json['status'] = 0;
            $json['message'] = lang('fail');
            $json['data'] = '请确认是否已开启php_socket扩展';
            echo json_encode($json);
            return;
        }
        $fp = @fsockopen($host,$port);
        if(!$fp){
            $json['status'] = 0;
            $json['message'] = lang('fail');
            $json['data'] = '请确认是否已开启socket服务(workman文件)';
            echo json_encode($json);
            return;
        }
        //创建tcp套接字
        $socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        //设置阻塞模式
        socket_set_block($socket);
        //连接tcp地址+端口
        socket_connect($socket, $host, $port);
        //向打开的套集字写入数据（发送数据）
        $msg = socket_write($socket, $act, $length);
        //接收返回的数据
        $recv_data = socket_read($socket, 1024);
        //关闭连接
        socket_close($socket);
        if ($msg) {
            $json['status'] = 1;
            $json['message'] = lang('success');
            $json['data'] ='接收到返回数据:'.bin2hex($recv_data);
            echo json_encode($json);
            return;
        }
        $json['status'] = 0;
        $json['message'] = lang('fail');
        $json['data'] = '请确认是否已开启socket服务(workman文件)';
        echo json_encode($json);
        return;


    }

}
